package top.lcm889.oj.leetcodeTop200;

import top.lcm889.oj.databean.Node;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author Coder-lcm
 * @date 2021/4/10
 */
public class LC429_N叉树的遍历 {
    public List<List<Integer>> levelOrder(Node root) {
        if (root == null) return new ArrayList<>();
        List<List<Integer>> res = new ArrayList<>();
        //每一层的树节点
        Queue<Node> floor = new LinkedList<>();
        // 把根节点移入遍历队列当中
        floor.offer(root);
        while (!floor.isEmpty()) {
            int size = floor.size();
            List<Integer> cur = new LinkedList<Integer>();
            for (int i = 0; i < size; i++) {
                Node node = floor.poll();
                cur.add(node.val);
                if (node.children != null && node.children.size() > 0) {
                    for (Node child : node.children) {
                        floor.offer(child);
                    }
                }
            }
            res.add(cur);
        }
        return res;

    }
}
